package cn.dylan.pet.service.impl;

import cn.dylan.basic.query.BaseQuery;
import cn.dylan.basic.service.impl.BaseServiceImpl;

import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.PageList;
import cn.dylan.pet.domain.Pet;
import cn.dylan.pet.domain.PetDetail;
import cn.dylan.pet.mapper.PetDetailMapper;
import cn.dylan.pet.mapper.PetMapper;
import cn.dylan.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;


    @Override
    public void update(Pet pet) {
        super.update(pet);
        petDetailMapper.update(pet.getPetDetail());
    }


    @Override
    public void save(Pet pet) {
        pet.setState(0);
        // 保存pet 返回自增长主键
        super.save(pet);
        //存petDetail 先把自增的主键存PetDetail对象
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet_id(pet.getId());
        petDetailMapper.save(petDetail);
    }

    @Override
    public PetDetail LoadPetDetailById(Long id) {
        return petDetailMapper.LoadByPetId(id);
    }

    @Override
    public JsonResult onSale(List<Long> ids) {
        try {
            Date date = new Date();
            petMapper.onSale(ids,date);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().setMsg("网络异常，稍后重试");
        }
    }

    @Override
    public JsonResult offSale(List<Long> ids) {
        try {
            Date date = new Date();
            petMapper.offSale(ids,date);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().setMsg("网络异常，稍后重试");
        }
    }

    @Override
    public PageList<Pet> queryPage(BaseQuery query) {
        return super.queryPage(query);
    }
}
